<?php
#
# author: Cody Sorgenfrey
# ©2013 South Hill Calvary Chapel
#
include("../common.php");

$title = "Where Do I Go From Here?";
print_html_head($title, NULL, NULL);
print_java_head(NULL);
?>
	<style>
		#loginBox{
			width: 200px;
			height: 140px;
			text-align: center;
			padding-top: 30px;
			margin-right: auto;
			margin-left: auto;
			border-radius: 5px;
		}
		#loginError{
			color: red;
		}
		#noPass{
			margin-top: 10px;
			font-size: 80%;
			text-align: center;
		}
		h4{
			margin-top: 10px;
		}
		p.em{
			margin-top: 7px;
			font-style: italic;
		}
		.hidden{
			display: none;
		}
		textarea{
			width: 60%;
			height: 100px;
			max-width: 98%;
			min-width: 200px;
		}
		.incomplete{
			background-color: #ffc8c8;
		}
		span#error{
			color: red;
			margin-left: 10px;
		}
		span#sending{
			color: black;
		}
	</style>
	<script type="text/javascript">
		var formID = new String;
		$(document).ready(function(){
			$("textarea, input").blur(function(){
				updateDB();
			});
			if (document.cookie.search(/churchMembership\=1\;/) > -1){
				if (document.location.search.search(/^\?id\=[0-9]+$/) > -1){
					formID = parseInt(document.location.search.split("=")[1]);
					loadOldForm();
				}
			}
		});
		function loadOldForm(){
			$.post("../mysql/data.php", {
				key: "<?echo crypt("He1sR1sen");?>",
				table: "churchMembershipFormReplies",
				limit: "1",
				searchField: "id",
				searchString: formID
			}, function(res){
				if (res.split("|cell|")[8] == "1"){
					$(":input").attr("disabled", "true");
				}
				var responses = res.split("|cell|")[2];
				responses = responses.split("<b>");
				for (x=0;x<responses.length;x++){
					var question = responses[x].split("</b>")[0];
					var answer = String(responses[x].split("</b>")[1]);
					answer = answer.substring(4, (answer.length - 8));
					$("p.question").each(function(){
						if ($(this).text().replace("  ", " ") == question.replace("  ", " ")){
							if ($(this).next(":input").is("input")){
								if (answer.search("<br>") > -1){
									answer = answer.split("<br>");
									$(this).nextUntil("p.question").filter("input").each(function(i){
										$(this).val(answer[i]);
									});
								} else{
									$(this).next("input").val(answer);
								}	
							} else if ($(this).next(":input").is("textarea")) {
								answer = answer.replace(/<br \/>/g, "");
								$(this).next("textarea").val(answer);
							} else if ($(this).next(":input").is("select")) {
								$(this).next("select").val(answer);
								if (question == "How many children do you have?"){
									updateChildren(answer);
									var kids = String(responses[x+1].split("</b>")[1]);
									kids = kids.substring(4, (kids.length - 8));
									kids = kids.split("<br>");
									$("div.child").each(function(i){
										var ageOffset = kids[i].search(/[0-9]+\/[0-9]+\/[0-9]+$/);
										var kidsAge = kids[i].substring(ageOffset);
										var kidsName = kids[i].substring(-1, ageOffset);
										$(this).children("input:eq(0)").val(kidsName);
										$(this).children("input:eq(1)").val(kidsAge);
									});
								}
							} else {
								$(this).next(".radios").children("input[value='"+answer+"']").attr("checked", "checked");
								if (answer == "Yes"){
									var newAnswer = 0;
								} else {
									var newAnswer = 1;
								}
								toggleNextHidden(($(this).next(".radios").children("input[value='"+answer+"']")), newAnswer);
							}
						}
					});
				}
			});
		}
		function updateDB(){
			if (formID == ""){
				getFormID();
			} else {
				sendForm("0");
			}
			$("#formLink").val("http://www.southhillcalvary.org/churchMembership/form.php?id="+formID);
		}
		function getFormID(){
			$.post("../mysql/addRow.php",{
				key: "<?echo crypt("He1sR1sen");?>",
				table: "churchMembershipFormReplies"
			}, function(res){
				if (res.search("Success") >= 0){
					formID = res.split(".")[1];
					updateDB();
				} else {
					alert("Error getting new ID, please contact the webservant.\n"+res);
				}
			});
		}
		function showNextHidden(elem){
			$(elem).parent().nextAll().filter(".hidden:eq(0)").show();
		}
		function hideNextHidden(elem){
			$(elem).parent().nextAll().filter(".hidden:eq(0)").hide();
		}
		function toggleNextHidden(elem, answer){
			$(elem).parent().nextAll().filter(".toggle:eq(0)").children().hide();
			$(elem).parent().nextAll().filter(".toggle:eq(0)").children(":eq("+answer+")").show();
		}
		function updateChildren(num){
			if (num == 0){
				$("#children").hide();
			} else {
				$("#children").show();
			}
			var currentChildren = $("#children").children().length - 1;
			if (currentChildren < num){
				for (var i=currentChildren;i<num;i++){
					$("#children").append("<div class='child'><input type='text' class='required' placeholder='First & Last' /><input type='text' class='required' placeholder='Month/Day/Year' /><br></div>");
				}
			} else if (currentChildren > num) {
				for (var i=currentChildren;i>num;i=i-1){
					$("#children").children(".child:last").remove();
				}
			}
		}
		function validateForm(){
			$(".required:visible").each(function(){
				if ($(this).val() == ""){
					$(this).addClass('incomplete');
				} else {
					$(this).removeClass('incomplete');
				}
			});
			$("div.radios").each(function(){
				if ($(this).children(":checked").length > 0){
					$(this).removeClass("incomplete");
				}
			});
			if ($(".incomplete").length > 0){
				$("span#error").text("Some required fields are not filled out.");
				return 0;
			} else {
				$("span#error").text("");
				return 1;
			}
		}
		function sendForm(sendMail){
			var questions = new Array;
			var answers = new Array;
			$("p.question:visible").each(function(){
				questions.push($(this).text());
				var answer = new Array;
				$(this).nextUntil("p.question").each(function(){
					if ($(this).is("div.radios")){
						answer.push($(this).children(":checked").val());
					} else if ($(this).is("div.child")){
						answer.push($(this).children("input:eq(0)").val() + " " + $(this).children("input:eq(1)").val())
					} else if ($(this).val() != "") {
						answer.push($(this).val());
					}
				});
				answers.push(answer.join("<br>"));
			});
			var questionString = questions.join("|Q|");
			var answerString = answers.join("|A|");
			$.post("submit.php", {
				questions: questionString, 
				answers: answerString, 
				id: formID, 
				sendMail: sendMail
			}, function(res){
				$("#error").html("");
				if (res.search("success") > -1){
					if (sendMail == "1"){
						$("div.content").html("<h4>Thank You</h4><p>We've recieved your questionairre and will be in touch shortly!</p>");
					}
				} else {
					prompt("Error "+res+"\nPlease email webservant@southhillcalvary.org with the following:", questionString + " " + answerString);
				}
			});
		}
		function finalSubmit(){
			if (!validateForm()){
				return 0;
			}
			sendForm("1");
			$("#error").html("<span id='sending'>Sending...</span>");
		}
		function countWords(obj){
			var wordCount = $(obj).val().split(" ").length;
			$("span#wordcount").text(wordCount+"/300 words");
			if (wordCount >= 300){
				$(obj).attr("maxlength", $(obj).val().length);
			} else {
				$(obj).removeAttr("maxlength");
			}
		}
	</script>
<?
begin_body();
page_title($title);
if ($_POST['pass'] == "26582"){
	?>
	<script type="text/javascript">
		document.cookie = "churchMembership=1";
		document.location = document.location;
	</script>
	<?
} else if (!empty($_POST['pass'])){
	?>
	<script type="text/javascript">
		document.location.search = "?loginError=Incorrect+Password";
	</script>
	<?
}
if (empty($_COOKIE['churchMembership'])){
	?>
	<div id='loginBox'>
	<form method="post">
	<h3>Password</h3><br>
	<input name='pass' type="password" id='loginPassword'><br><br>
	<button>Login</button><br>
	<p id="loginError"><?echo $_GET['loginError']?></p>
	</form>
	</div>
	<p id='noPass'>Do you need the password? <a href="mailto:webservant@southhillcalvary.org?subject=Password%20for%20directory">Contact the Webservant</a></p>
	<?
	end_page_title();
	end_body();
	die();
}
?>
			<p>Our greatest desire as a leadership of South Hill Calvary Chapel is two-fold. First to minister to your spiritual, physical, and emotional needs and secondly to help you grow in how you can best minister to those same needs in others.<br><br>
			If at any time after you've started the form you'd like to come back to it, this link will bring you back to your form: <input id="formLink" type="text" /><br><br>
			Please take some thoughtful time to answer the following questions:</p>
			<h4>General Questions...</h4>
			<p class='question'>What is your first and last name? Do you have any nicknames by which you prefer to be called?</p>
			<input type="text" class="required" placeholder="First & Last" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" placeholder="Nickname" />
			<p class='question'>What is your phone number?</p>
			<input type="text" class="required" />
			<p class='question'>What is your e-mail address?</p>
			<input type="text" class="required" />
			<p class='question'>What is your home address?</p>
			<textarea class="required"></textarea>
			<h4>Tell us about yourself…</h4>
			<p class="em">It is important for us to have the opportunity to know you, so that we might best know how to serve you and encourage you in service to others. Please tell us as much about yourself as you feel comfortable. The more you tell us, the better we will know you.</p>
			<p class='question'>What do you enjoy doing in your spare time? (sports, reading, writing, gardening)</p>
			<textarea class="required"></textarea>
			<p class='question'>What is your educational background?</p>
			<textarea class="required"></textarea>
			<p class='question'>What is your occupation?</p>
			<input type="text" class="required" />
			<p class='question'>What is your birth date?</p>
			<input type="text" placeholder="Month/Day/Year" class="required" />
			<p class='question'>Are there any special achievements of which you are particularly proud?</p>
			<textarea></textarea>
			<h4>Tell us about a few of your favorite things…</h4>
			<p class="em">One of the things the leadership doesn’t often have the opportunity to do is get to know the things that people in the church body really enjoy. Your answers to these questions will tell us a little about your interests, which will provide some interesting things to talk about when you meet with one of the elders.</p>
			<p class='question'>List your three favorite books. (not including the Bible)</p>
			<input type="text" class="required" /><input type="text" class="required" /><input type="text" class="required" />
			<p class='question'>List your three favorite movies.</p>
			<input type="text" class="required" /><input type="text" class="required" /><input type="text" class="required" />
			<p class='question'>List your three favorite music artists.</p>
			<input type="text" class="required" /><input type="text" class="required" /><input type="text" class="required" />
			<p class='question'>List your three favorite Bible teachers.</p>
			<input type="text" class="required" /><input type="text" class="required" /><input type="text" class="required" />
			<h4>Tell us about your immediate family…</h4>
			<p class="em">Please take advantage of the last question in this section and write as much as you want about your family. Here is another chance for you to brag a bit. This will be especially easy if you’re a grandparent.</p>
			<p class='question'>If you are married, when is your anniversary?</p>
			<input type="text" placeholder="Month/Day/Year" />
			<p class='question'>How many children do you have?</p>
			<select onchange="updateChildren($(this).val())">
				<option>0</option>
				<option>1</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
				<option>5</option>
				<option>6</option>
				<option>7</option>
				<option>8</option>
				<option>9</option>
				<option>10</option>
				<option>11</option>
				<option>12</option>
				<option>13</option>
				<option>14</option>
				<option>15</option>
			</select>
			<div id='children' class="hidden">
				<p class='question'>What are their names and birth dates?</p>
			</div>
			<p class='question'>How many grandchildren do you have?</p>  
			<select>
				<option>0</option>
				<option>1</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
				<option>5</option>
				<option>6</option>
				<option>7</option>
				<option>8</option>
				<option>9</option>
				<option>10</option>
				<option>11</option>
				<option>12</option>
				<option>13</option>
				<option>14</option>
				<option>15+</option>
			</select>
			<p class='question'>Is there anything special you would like to share about your family?</p>
			<textarea></textarea>
			<h4>Tell us about your spiritual journey…</h4>
			<p class="em">It is important that you tell us what is really on your heart here. Don’t be afraid to be a little vulnerable. No one but the eldership of the church will see your answers unless we have your permission. Your spiritual growth is of utmost importance to us. Fight the temptation to tell us what you think you want us to hear rather than what is really going on.</p>
			<p class='question'>Based on what you have been learning in church and in these classes, would you consider yourself to be a Christian?</p>
			<div class="radios required">
				<input type="radio" value='Yes' name="christian" onclick="toggleNextHidden(this, 0)"> Yes <br>
				<input type="radio" value='No' name="christian" onclick="toggleNextHidden(this, 1)"> No
			</div>
			<div class="toggle">
				<div class="hidden">
					<p>Share your story of becoming a follower of Jesus Christ:</p>
					<p class='question'>Life before Christ (300 words or less):</p>
					<textarea onkeyup="countWords(this)" class='required'></textarea><br><span id='wordcount'>0/300 words</span>
					<p class='question'>How you came to put your trust in Christ:</p>
					<textarea class="required"></textarea>
					<p class='question'>How your faith has grown since trusting Christ:</p>
					<textarea class="required"></textarea>
					<p class='question'>When would you say you were born again?</p>
					<textarea class="required"></textarea>
					<p class='question'>What is God teaching you right now?</p>
					<textarea class="required"></textarea>
				</div>
				<div class="hidden">
					<p class='question'>How would you describe your relationship with God?</p>
					<textarea class="required"></textarea>
				</div>
			</div>
			<p class='question'>Where do you think you need the most spiritual growth?</p>
			<textarea class="required"></textarea>
			<p class='question'>Are you struggling with a particular issue in which you need spiritual or Biblical help? <br>Add as much detail as you're comfortable.</p>
			<textarea></textarea>
			<h4>Tell us about your church experience…</h4>
			<p class="em">There are all kinds of church experiences. Some are good, and others not so good. We want to be sensitive to your expectations and your fears (based on past experiences). Don’t be concerned with telling us something that you may feel might be critical. We can take it. We can’t improve how we care for the flock unless we know what things we need to improve on.</p>
			<p class='question'>Where have you been a part of a local church in the past?</p>
			<textarea class="required"></textarea>
			<p class='question'>What things do you feel are most important to be present in your home church?</p>
			<textarea class="required"></textarea>
			<p class='question'>Have you felt welcomed here?</p>
			<div class="radios required">
				<input type="radio" value='Yes' name="welcomed" onclick="toggleNextHidden(this, 0)"> Yes <br>
				<input type="radio" value='No' name="welcomed" onclick="toggleNextHidden(this, 1)"> No
			</div>
			<div class="toggle">
				<div class="hidden">
					<p class='question'>What made you feel welcomed?</p>
					<textarea class="required"></textarea>
				</div>
				<div class="hidden">
					<p class='question'>Why not?</p>
					<textarea class="required"></textarea>
				</div>
			</div>
			<p class='question'>Has it been easy to build relationships here?</p>
			<div class="radios required">
				<input type="radio" value='Yes' name="grandChildren" onclick="hideNextHidden(this)"> Yes <br>
				<input type="radio" value='No' name="grandChildren" onclick="showNextHidden(this)"> No
			</div>
			<div class="hidden">
				<p class='question'>What could be done to facilitate the building of relationships?</p>
				<textarea class="required" ></textarea>
			</div>
			<p class='question'>Why have you chosen to fellowship with us?</p>
			<textarea class="required" ></textarea>
			<p class='question'>What questions do you have for the leadership at South Hill Calvary Chapel?</p>
			<textarea class="required" ></textarea>
			<h4>Tell us where you best see yourself serving…</h4>
			<p class="em">Our goal is to know you well enough that you can serve where there is the best fit. We realize that there are times when members need to serve in areas of need. However, our goal is to know your gifts, talents, and abilities well enough that we know where your service to the Lord might best be used.</p>
			<p class='question'>If you have served in ministry in the past, what are the areas in which you have served? <br>(both inside and outside the local church)</p>
			<textarea class="required" ></textarea>
			<p class='question'>What would you say are the gifts or interests that God has given you?</p>
			<textarea class="required" ></textarea>
			<h4>Tell us about your involvement at South Hill Calvary Chapel…</h4>
			<p class="em">Some of you are completely new, others of you have been here for awhile. Some of you are still in that decision process of whether or not South Hill Calvary Chapel is going to be your home church. Regardless of where you fit it, we want to know if and how you have been involved. Don’t worry if you are unable to answer a portion or any of these questions.</p>
			<p class='question'>In which events have you participated and which ones have you enjoyed the most?</p>
			<textarea class="required" ></textarea>
			<p class='question'>Which church events do you feel are most important in fostering healthy body life?</p>
			<textarea class="required" ></textarea>
			<p class='question'>Are you a part of a Home Fellowship?</p>
			<div class="radios required">
				<input type="radio" value='Yes' name="HF" onclick="toggleNextHidden(this, 0)"> Yes <br>
				<input type="radio" value='No' name="HF" onclick="toggleNextHidden(this, 1)"> No
			</div>
			<div class="toggle">
				<div class="hidden">
					<p class='question'>Which one do you attend?</p>
					<textarea class="required"></textarea>
				</div>
				<div class="hidden">
					<p class='question'>Would you like to attend one?</p>
					<textarea class="required"></textarea>
					<p class='question'>Do you need help choosing the right one?</p>
					<textarea class="required"></textarea>
				</div>
			</div>
			<h4>Wrap up…</h4>
			<p class="em">Here is where you can tell us anything you want, that you haven’t been able to say up until this point. We truly look forward to meeting with you and discussing your answers in person.</p>
			<p class='question'>Is there anything else you would like to share with us?</p>
			<textarea></textarea>
			<p>Once again, thank you for answering these questions. We will be in contact with you soon to continue getting to know you better</p>
			<button onclick="finalSubmit();">Send</button><span id='error'></span>
<?
end_page_title();
end_body();
?>